import { v4 as uuidv4 } from "uuid";
import { config } from "dotenv";
import axios from "axios";
config();

import { deleteVerificationTokenByEmail } from "@/data/verification-token";

export const generatePasswordResetToken = async (email: string) => {
  const token = uuidv4();
  return token;
};

export const generateVerificationToken = async (email: string) => {
  const token = uuidv4();
  const expires = new Date(new Date().getTime() + 3600 * 1000);

  await deleteVerificationTokenByEmail(email);
  const data = {
    email,
    token,
    expires,
  };
  console.log(`back api server ${process.env.BACK_SERVER_API_URL}`);
  const tokenResp = await axios
    .post(`${process.env.BACK_SERVER_API_URL}/user/token`, data)
    .then((response) => {
      return response.data; // The response data
    })
    .catch((error) => {
      console.error("Error fetching data:", error);
      return null;
    });
  return tokenResp;
};
